﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.BLL;
using Shanfree.SharpMovie.Model;
using SIRC.Framework.Net;
using System.IO;

namespace SharpMClient
{
    public partial class AddMovie : Form
    {
        public AddMovie()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void AddMovie_Load(object sender, EventArgs e)
        {
            try
            {
                IList<NationInfo> list = new Nation().GetList();
                cblNation.DataSource = list;
                cblNation.DisplayMember = "Name";
                cblNation.ValueMember = "ID";
            }
            catch (Exception ex)
            {
                LogEntry.Log.Write(ex.ToString(), LogSourceType.一般错误);
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string awards = tbAwards.Text;
                string chineseName = tbChineseName.Text;
                string director = tbDirector.Text;
                string englishName = tbEnglishName.Text;
                string hongkongName = tbHongkongName.Text;
                string IMDB_ID = tbIMDB_ID.Text;
                string introduce = tbIntroduce.Text;
                string leadingRule = tbLeadingRule.Text;
                string originalName = tbOriginalName.Text;
                string postLink = tbPostLink.Text;
                string produceYear = tbProduceYear.Text;
                string userID = "1";
                string movieID = new Movie().Add(IMDB_ID, originalName, englishName, chineseName, hongkongName, postLink, produceYear, director, leadingRule, introduce, awards, userID);
                for (int i = 0; i < cblNation.Items.Count; i++)
                {
                    if (cblNation.GetItemChecked(i) == true)
                    {
                        string nationID = ((NationInfo)cblNation.Items[i]).ID;
                        new Movie().AddNation(movieID, nationID);
                    }
                }
            }
            catch (Exception ex)
            {
                LogEntry.Log.Write(ex.ToString(), LogSourceType.一般错误);
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {

                string imdbNo = tbIMDB_ID.Text;
                EntityXpathInfo eInfo = GetMappingFromFile(imdbNo);
                MovieInfo mInfo = new EntityInitialHander<MovieInfo>().GetEntityFromXpath(eInfo);

                // 持久化
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"tt0111161_model.xml");
                SerializeHandler<MovieInfo>.SerializeToFilePath(mInfo, filePath);

                tbAwards.Text = mInfo.Awards;
                tbChineseName.Text = mInfo.ChineseName;
                pbPost.ImageLocation = mInfo.PostLink;
                tbDirector.Text = mInfo.Director;
                tbEnglishName.Text = mInfo.EnglishName;
                tbHongkongName.Text = mInfo.HongkongName;
                tbIMDB_ID.Text = mInfo.IMDB_ID;
                tbIntroduce.Text = mInfo.Introduce;
                tbLeadingRule.Text = mInfo.LeadingRule;
                tbOriginalName.Text = mInfo.OriginalName;
                tbPostLink.Text = mInfo.PostLink;
                tbProduceYear.Text = mInfo.ProduceYear;

                string[] nArray = mInfo.NationListString.Split(',');
                SetNationCheck(nArray);
            }
            catch (Exception ex)
            {
                LogEntry.Log.Write(ex.ToString(), LogSourceType.一般错误);
                MessageBox.Show(ex.ToString());
            }
        }

        private void SetNationCheck(string[] nArray)
        {
            // 待初始化
            for (int i = 0; i < cblNation.Items.Count; i++)
            {
                // 重置选定的国家
                cblNation.SetItemChecked(i, false);
                string eName = ((NationInfo)cblNation.Items[i]).EnglishName;
                for (int j = 0; j < nArray.Length; j++)
                {
                    if (nArray[j].Trim() == eName)
                    {
                        cblNation.SetItemChecked(i, true);
                    }
                }
            }
        }
        /// <summary>
        /// 加载映射文档
        /// </summary>
        /// <returns></returns>
        private EntityXpathInfo GetMappingFromFile(string imdbNo)
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EntityMapping.xml");
            EntityXpathInfo eInfo = SerializeHandler<EntityXpathInfo>.DeSerializeFromFile(filePath);
            eInfo.CustomerString = imdbNo;
            //http://www.imdb.com/title/tt0111161/
            string url = string.Format(eInfo.URL, eInfo.CustomerString);
            eInfo.URL = url;
            return eInfo;
        }
    }
}
